package com.gj.demojuc.chap08;

import java.util.concurrent.*;

/**
 * 自定义线程池 7个参数
 *
 */
public class CustomizeThreadPoolDemo {
    public static void main(String[] args) {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2,
                5,
                2,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(10),
                Executors.defaultThreadFactory(),
//                new ThreadPoolExecutor.AbortPolicy()
                new RejectedExecutionHandler() {
                    @Override
                    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                        try {
                            Thread.sleep(2000);
                            executor.submit(r);//休息两秒重新将任务交给线程池处理
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
        );
        for (int i = 0; i < 16; i++) {
            String num = String.valueOf(i+1);
            Future<Integer> fu = threadPoolExecutor.submit(() -> {
                System.out.println(Thread.currentThread().getName() + "come in" + num);
                return 100;
            });

//            try {
//                Integer result = fu.get();
//                System.out.println(result);
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            } catch (ExecutionException e) {
//                throw new RuntimeException(e);
//            }
        }
//        threadPoolExecutor.shutdown();
    }

}
